﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using TRPOP.Common;
using TRPOP.CMS.Web.UI;


using TRPOP.CMS.Business;
using TRPOP.CMS.Model;
using Newtonsoft.Json;
using System.Data;

namespace TRPOP.CMS.Web.Admin
{
    public partial class editContentLink_list : BaseAdmin
    {
        #region 当前页面权限验证
        protected override void SubPageInit()
        {
            base.m_SubPagePermission = Plugin.PermissionKeys.PermissionEnum.Content_AllControl;
        }
        #endregion


        #region 页面变量
        protected bool m_IsAdd = true;
        protected int m_intNodeId = 0;
        protected int m_intId = 0;
        protected string m_strParentName = "";
        protected string m_strNodeName = "";
        protected NodeInfo m_NodeInfo = new NodeInfo();
        protected NodeManage m_NodeManage = new NodeManage();
        protected ContentManage m_ContentManage = new ContentManage();
        protected string m_strAction = "";
        protected int m_intRecordCount = 0;
        private DataSet ds = new DataSet();
        private List<ModelFieldInfo> TitleList = new List<ModelFieldInfo>();
        private string strListFields = "";
        private int m_intPubStatus = 0;
        private string m_strSearchDate = "";
        private string m_strSearchKey = "";
        #endregion

        #region 页面事件
        protected void Page_Load(object sender, EventArgs e)
        {
            m_intNodeId = RequestHelper.RequestInt("nodeid");
            m_strAction = RequestHelper.RequestString("ac");
            m_intPubStatus = RequestHelper.RequestInt("state", 100);
            m_strSearchDate = RequestHelper.RequestString("Date");
            m_strSearchKey = RequestHelper.RequestString("Keywords").Replace("'", "");

            if (!IsPostBack)
            {
                LoadBase();
                BindData();
            }
        }

        #endregion

        #region 页面方法

        public void LoadBase()
        {

           
          

            #region
            TitleList = new List<ModelFieldInfo>();
            ModelFieldInfo m_info = (new ModelFieldManage()).GetTableTitleFieldByNodeId(m_intNodeId);
            TitleList.Add(m_info);
            repTitleList.DataSource = TitleList;
            repTitleList.DataBind();
            for (int i = 0; i < TitleList.Count; i++)
            {
                strListFields += "," + TitleList[i].FieldName;
            }
            #endregion

        }

        public void BindData()
        {
            if (m_intNodeId > 0)
            {
                string strDateCondition = "";
                string strExtId = "";

                m_NodeInfo = m_NodeManage.GetInfoById(m_intNodeId);
                if (m_NodeInfo != null)
                {
                    #region 条件
                    m_strNodeName = "[" + m_NodeInfo.Name + "]";

                    Pager.PageSize = RequestHelper.RequestInt("offset", SYSConfig.SystemVar_AdminContentPageNum);
                    if (Pager.PageSize > SYSConfig.SystemVar_PubPageListMax)
                    {
                        Pager.PageSize = SYSConfig.SystemVar_PubPageListMax;
                    }
                    string strPubStatus = "";
                    switch (m_intPubStatus)
                    {
                        case 0:
                        case 1:
                            strPubStatus = " and PubStatus=" + m_intPubStatus;
                            break;
                        default:
                            strPubStatus = " and PubStatus >=0 ";
                            break;
                    }
                    #region 发布时间
                    if (m_strSearchDate != "")
                    {
                        try
                        {
                            DateTime dtTime = Convert.ToDateTime(m_strSearchDate);
                            strDateCondition = " and PubTime>='" + dtTime.ToString() + "' and PubTime <'" + dtTime.AddDays(1).ToString() + "' ";
                        }
                        catch
                        {

                        }
                    }
                    #endregion

                    #region 关键字
                    if (m_strSearchKey != "")
                    {
                        SearchKeywords.Text = m_strSearchKey;
                        strExtId = m_ContentManage.GetExtendIdBySearchKey(m_strSearchKey, m_intNodeId);
                        if (strExtId != "")
                        {
                            strExtId = " and ExtendId in (" + strExtId + ")";
                        }
                    }
                    #endregion

                    #endregion

                    if (Pager.PageSize <= 0) { Pager.PageSize = SYSConfig.SystemVar_AdminContentPageNum; }
                    rptList.DataSource = m_ContentManage.GetListByPager(Pager.CurrentPage, Pager.PageSize, strExtId + strDateCondition + strPubStatus + "  and NodeId=" + m_intNodeId + " and [Type]=" + (int)LinkType.EnumElement.RealLink, " PubStatus desc,TopWeight desc,PubTime desc,DisplayOrder desc", out m_intRecordCount);
                    rptList.DataBind();
                    Pager.ShowRecordCount = true;
                    Pager.ShowPageSize = true;
                    Pager.ShowCurrentPage = true;
                    Pager.RecordCount = m_intRecordCount;
                }

            }

        }

        protected string BindContentTableData(object tableName, object id, object ContentId, object PubStatus, object Type, object TopWeight, object EliteWeight)
        {
            string strTD = "";
            string strSelFields = "";
            if (strListFields != "")
                strSelFields = tableName.ToString() + "id" + strListFields;
            ds = m_ContentManage.GetDataSet(tableName.ToString(), strSelFields, " and " + tableName.ToString() + "id=" + id, "");
            if (ds.Tables.Count > 0)
            {
                if (ds.Tables[0].Rows.Count > 0)
                {
                    DataRow dr = ds.Tables[0].Rows[0];
                    for (int i = 0; i < TitleList.Count; i++)
                    {
                        string strVal = "", strFieldName = "";
                        ModelFieldInfo oModelFieldInfo = TitleList[i];
                        strFieldName = oModelFieldInfo.FieldName;
                        strVal = dr[strFieldName].ToString();
                        if (strVal != "")
                        {
                            #region
                            FormInputTypeInfo oFormInputTypeInfo = (new FormInputTypeManage()).GetInfoByIdFromCache(oModelFieldInfo.FormInputTypeId);
                            if (oModelFieldInfo.FieldTypeStr != "bit")
                            {
                                if (oFormInputTypeInfo.FormItem == "select" || oFormInputTypeInfo.FormItem == "radio" || oFormInputTypeInfo.FormItem == "checkbox")
                                {
                                    strVal = (new FormInputValueManage()).GetValueNameByIDs(strVal);
                                }
                            }
                            #endregion
                        }

                        if (TitleList[i].IsTitleField)
                        {
                            #region

                            strTD += "<td>";
                            strTD += "<a id=\"Title_" + ContentId + "\"  href=\"javascript:void(0);\" onclick=\"doing('addContentLink','" + ContentId + "')\" ";                   
                            strTD += " title=\"" + strVal + "\">";
                            strTD += StringExtension.CutStringWithByte(strVal, SYSConfig.SystemVar_ListTitleLength) + "</a>";
                            strTD += "</td>";
                            #endregion
                        }
                        else
                        {
                            strTD += "<td>" + strVal + "</td>";
                        }
                    }
                }
            }
            return strTD;
        }

        #endregion


    }
}
